package com.miracle.moa.manager.controller;


import com.miracle.common.Result;
import com.miracle.common.StatusCode;
import com.miracle.moa.manager.entity.Permission;
import com.miracle.moa.manager.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author miracle
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/manager/permission")
public class PermissionController {


    @Autowired
    private IPermissionService permissionService;
    /**
     * 根据用户查询用户权限
     * @return
     */
    @GetMapping("/queryPermissionByUser")
    public Result<Map<String, Object>> queryPermissionByUser() {
        Map<String, Object> result = permissionService.queryPermissionByUser();
        return new Result<>(StatusCode.OK, "查询成功", result);
    }


    /**
     * 查询权限树
     * @param keyword 权限名称
     * @return
     */
    @GetMapping("/listPermissionTree")
    @PreAuthorize("hasAnyAuthority('admin')")
    public List<Permission> listPermissionTree(@RequestParam(name = "keyword") String keyword) {
        return permissionService.listPermissionTree(keyword);
    }

    /**
     * 查询权限树
     * @return
     */
    @GetMapping("/listPermissionTreeWithBtn")
    @PreAuthorize("hasAnyAuthority('admin')")
    public List<Permission> listPermissionTreeWithBtn() {
        return permissionService.listPermissionTreeWithBtn();
    }



    /**
     * 保存权限信息
     * @param permission
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result<Permission> save(@Validated @RequestBody Permission permission) {
        LocalDateTime now = LocalDateTime.now();

        permission.setUpdateTime(now);
        permission.setCreateTime(now);

        permissionService.save(permission);

        return new Result<>(StatusCode.OK, "保存成功", permission);
    }

    /**
     * 按钮权限维护
     * @param permission
     * @return
     */
    @PostMapping("/editBtnPerms")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result editBtnPerms(@RequestBody Permission permission) {
        return permissionService.saveOrUpdatePermissions(permission.getBtnPerms(), permission.getParentId());
    }

    /**
     * 保存权限信息
     * @param permission
     * @return
     */
    @PostMapping("/edit")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result<Permission> edit(@Validated @RequestBody Permission permission) {
        LocalDateTime now = LocalDateTime.now();

        permission.setUpdateTime(now);
        permissionService.updateById(permission);
        return new Result<>(StatusCode.OK, "更新成功", permission);
    }

    /**
     * 根据id删除权限
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result delete(@PathVariable int id) {

        return permissionService.deletePermissionById(id);
    }

}
